﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace bzxxxt.bll
{
    public class CommUtil
    {
        public static DateTime? ToDateTime(string str) {
            if (string.IsNullOrEmpty(str))
            {
                return null;
            }
            else {
                try {
                    return DateTime.Parse(str);
                }
                catch (Exception ex) {
                    return null;
                }
            }
        }

        public static string ToString(DateTime dt) {
            if (dt == DateTime.MinValue) return "";
            return dt.ToString("yyyy-MM-dd HH:mm:ss");
        }

        public static int? ToInt(string str) {            
            try {
                return int.Parse(str);
            }
            catch (Exception ex) {
                return null;
            }
        }

        public static int GetPageSize(HttpContext context)
        {
            try
            {
                return int.Parse(context.Request["rows"].ToString());
            }
            catch
            {
                return 10;
            }
        }


        public static string GetOrder(HttpContext context)
        {
            try
            {
                string order = context.Request.Form["order"];
                return order== null ? "asc":order;
            }
            catch{
                return "asc";
            }
        }


        public static string GetSort(HttpContext context)
        {
            try
            {
                string sort =  context.Request.Form["sort"];
                return sort == null ? "id" : sort;
            }
            catch{
                return "id";
            }
        }

        public static int GetPageIndex(HttpContext context)
        {
            try
            {
                return int.Parse(context.Request["page"].ToString());
            }
            catch
            {
                return 1;
            }
        }
    }
}